package com.itheima.service.empl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.itheima.bean.*;
import com.itheima.common.OrderStatus;
import com.itheima.dao.OrderDao;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrderService;
import com.itheima.service.ShoppingCartService;
import com.itheima.utile.BaseContext;
import com.itheima.utile.StrLocalDataTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private HttpSession session;

    /**
     * 添加订单
     *
     * @param orders
     * @return
     */
    @Override
    public int add(Orders orders) {
        orders.setNumber(UUID.randomUUID().toString().replace("-", ""));
        orders.setStatus(2);
        User user = (User) session.getAttribute("user");
        orders.setUserId(user.getId());
        orders.setUserName(user.getName());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setOrderTime(LocalDateTime.now());
        int row = orderDao.insert(orders);
        List<ShoppingCart> shoppingCartList = shoppingCartService.findAll();
        int row1 = 0;
        for (ShoppingCart shoppingCart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orders.getId());
            BeanUtils.copyProperties(shoppingCart, orderDetail);
            row1 += orderDetailService.add(orderDetail);
        }
        int row2 = 0;
        if (row > 0 && row1 == shoppingCartList.size()) {
            row2 = shoppingCartService.delete();
        }
        return row2;
    }

    /**
     * 分页查询用户端订单
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public IPage<Orders> findPage(int page, int pageSize) {
        IPage<Orders> iPage = new Page<>(page, pageSize);
        return orderDao.selectPage(iPage, null);
    }

    /**
     * 分页查询到客户端
     *
     * @param baseOrder
     * @return
     */
    @Override
    public IPage<Orders> page(BaseOrder baseOrder) {
        IPage<Orders> page = new Page<>(baseOrder.getPage(), baseOrder.getPageSize());
        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        qw.like(baseOrder.getNumber() != null, Orders::getNumber, baseOrder.getNumber());
        if (baseOrder.getBeginTime() != null) {
            LocalDateTime BeginTime = StrLocalDataTime.strTime(baseOrder.getBeginTime());
            LocalDateTime EndTime = StrLocalDataTime.strTime(baseOrder.getEndTime());
            qw.ge(Orders::getOrderTime, BeginTime);
            qw.le(Orders::getOrderTime, EndTime);
        }
        return orderDao.selectPage(page, qw);
    }

    /**
     * 修改订单状态
     *
     * @param orders
     * @return
     */
    @Override
    public int updateStatus(OrderStatus orders) {

        Orders one = orderDao.selectById(orders.getId());

        int row=0;

        if (one != null) {

            one.setStatus(orders.getStatus());

            row = orderDao.updateById(one);
        }

        return row;
    }
}
